package cn.yotta.user.web.autoconfiguration.config;

import cn.yotta.user.web.autoconfiguration.filter.TenantContextFilter;
import cn.yotta.user.web.autoconfiguration.provider.TenantResolveProvider;
import cn.yotta.user.web.autoconfiguration.provider.impl.DefaultTenantResolveProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @author wyaoyao
 * @date 2022/11/8 17:08
 */
@Configuration
public class TenantWebContextConfiguration {

    @Bean
    public FilterRegistrationBean<Filter> registrationTenantFilterBean(TenantResolveProvider tenantLoadProvider) {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new TenantContextFilter(tenantLoadProvider));
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(TenantContextFilter.ORDER);
        return registrationBean;
    }

    @Bean
    @ConditionalOnMissingBean(TenantResolveProvider.class)
    public TenantResolveProvider tenantLoadProvider() {
        return new DefaultTenantResolveProvider();
    }

}
